/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.OpenParts.test.functionnal;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.thoughtworks.selenium.Selenium;

/**
 * Selenium functional test of Message Dispatching on the OpenParts Showcase
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.date 23 January 2011
 */
public class SeleniumTestMessageDispatching {

    @Test
    public void testMessageDispatching() throws InterruptedException {
        WebDriver driver = new FirefoxDriver();

        String baseUrl = "http://127.0.0.1:8080/";
        Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
        
        // load the page and test it's loaded
        selenium.open("/index.html");
        selenium.waitForPageToLoad("30000");
        assertTrue(selenium.isElementPresent("//body[@id='fr-openparts']"));
        assertTrue(selenium.isTextPresent("OpenParts Showcase"));
        
        // loading the page load the showcase module - test showcase module is loaded
        Thread.sleep(2000);
        assertTrue(selenium.isTextPresent("Message Dispatching"));
        
        // load the message container module and test that it's loaded
        selenium.click("//body[@id='fr-openparts']/div[2]/div[2]/div/div[2]/div/button[3]");
        Thread.sleep(2000);
        assertTrue(selenium.isTextPresent("Container for Message Dispatching"));
        assertTrue(selenium.isTextPresent("no message from view 1 yet"));
        assertTrue(selenium.isTextPresent("no message from view 2 yet"));
        assertTrue(selenium.isTextPresent("no message from view 3 yet"));
        
        // check that no views are displayed yet
        assertFalse(selenium.isTextPresent("Embedded view 1"));
        assertFalse(selenium.isTextPresent("Embedded view 2"));
        assertFalse(selenium.isTextPresent("Embedded view 3"));
        
        // display view 1 - test it's displayed and that it doesn't get any message from container
        selenium.click("//div[@id='OP-messagecontainer-messagecontainer-display1']/div/div[3]/div/div[2]/div/div[4]/div/button[1]");
        Thread.sleep(2000);
        assertTrue(selenium.isTextPresent("Embedded view 1"));
        assertTrue(selenium.isTextPresent("no message from container 1 yet"));
        
        // dispatch message from container to view 1 - test message is received (with params) and that container get a OK ACK
        selenium.click("//div[@id='OP-messagecontainer-messagecontainer-display1']/div/div[3]/div/div[2]/div/div[4]/div/button[2]");
        assertTrue(selenium.isTextPresent("message from container 1 params = 1"));
        assertTrue(selenium.isTextPresent("slot dispatch success 1"));
        
        // check that container didn't receive the message
        assertTrue(selenium.isTextPresent("no message from view 1 yet"));
        
        // dispatch message from view 1 to container - test message is received (with params) and that view get a OK ACK
        selenium.click("//div[@id='OP-messageembedded-messageembedded-display2']/div/div[4]/div/button");
        assertTrue(selenium.isTextPresent("message from view 1 params = 1"));
        assertTrue(selenium.isTextPresent("view dispatch success 1"));
        
        // display view 2 - test it's displayed and that it doesn't get any message from container
        selenium.click("//div[@id='OP-messagecontainer-messagecontainer-display1']/div/div[4]/div/div[2]/div/div[4]/div/button[1]");
        Thread.sleep(2000);
        assertTrue(selenium.isTextPresent("Embedded view 2"));
        assertTrue(selenium.isTextPresent("no message from container 2 yet"));
        assertTrue(selenium.isTextPresent("no message from view 2 yet"));
        
        // display view 3 - test it's displayed and that it doesn't get any message from container
        selenium.click("//div[@id='OP-messagecontainer-messagecontainer-display1']/div/div[5]/div/div[2]/div/div[4]/div/button[1]");
        Thread.sleep(2000);
        assertTrue(selenium.isTextPresent("Embedded view 3"));
        assertTrue(selenium.isTextPresent("no message from container 3 yet"));
        assertTrue(selenium.isTextPresent("no message from view 3 yet"));
        
        // dispatch message from container to view 2 - test message is received (with params) and that container get a OK ACK
        selenium.click("//div[@id='OP-messagecontainer-messagecontainer-display1']/div/div[4]/div/div[2]/div/div[4]/div/button[2]");
        assertTrue(selenium.isTextPresent("message from container 2 params = 2"));
        assertTrue(selenium.isTextPresent("slot dispatch success 2"));
        
        // check that that view 3 didn't get the message
        assertTrue(selenium.isTextPresent("no message from container 3 yet"));
        
        // check that no container get the message
        assertTrue(selenium.isTextPresent("no message from view 2 yet"));
        assertTrue(selenium.isTextPresent("no message from view 3 yet"));

        // dispatch message from view 3 to container - test message is received (with params) and that view get a OK ACK
        selenium.click("//div[@id='OP-messageembedded-messageembedded-display4']/div/div[4]/div/button");
        assertTrue(selenium.isTextPresent("message from view 3 params = 3"));
        assertTrue(selenium.isTextPresent("view dispatch success 3"));
        
        // check that that container didn't get the message
        assertTrue(selenium.isTextPresent("no message from view 2 yet"));
        
        // check that that view 3 didn't get the message
        assertTrue(selenium.isTextPresent("no message from container 3 yet"));
        
        // dispatch message from view 2 to container - test message is received (with params) and that view get a OK ACK
        selenium.click("//div[@id='OP-messageembedded-messageembedded-display3']/div/div[4]/div/button");
        assertTrue(selenium.isTextPresent("message from view 2 params = 2"));
        assertTrue(selenium.isTextPresent("view dispatch success 2"));
        
        // check that that view 3 didn't get the message
        assertTrue(selenium.isTextPresent("no message from container 3 yet"));
        
        // dispatch message from container to view 3 - test message is received (with params) and that container get a OK ACK
        selenium.click("//div[@id='OP-messagecontainer-messagecontainer-display1']/div/div[5]/div/div[2]/div/div[4]/div/button[2]");
        assertTrue(selenium.isTextPresent("message from container 3 params = 3"));
        assertTrue(selenium.isTextPresent("slot dispatch success 3"));
        
        selenium.stop();
    }

}
